package controller;

import java.sql.Date;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

import implementor.CategoryImp;
import implementor.PurchaseImp;
import implementor.SellerInfoImp;
import daoInterface.CategoryDao;
import daoInterface.PurchaseDao;
import daoInterface.SellerInfoDao;
import dto.Purchase;
import model.AccountReport;
import model.Category;
import model.CustomerReportItems;
import model.SellerInfo;
import model.AccountReportItems;
import utilities.ViewUtilites;
import views.AccountReportView;

public class AccountReportController {
	private AccountReportView view;
	private AccountReport model;
	
	public AccountReportController() {
	
	}

	public AccountReportController(AccountReportView view, AccountReport model) {
		super();
		this.view = view;
		this.model = model;
	}

	public AccountReportView getView() {
		return view;
	}

	public void setView(AccountReportView view) {
		this.view = view;
	}

	public AccountReport getModel() {
		return model;
	}

	public void setModel(AccountReport model) {
		this.model = model;
	}
	
	public void setCategoryCombo()
	{
		CategoryDao cd=new CategoryImp();
		Category[] c=cd.retrieveAll();
		String[] items=new String[c.length];
		for(int i=0;i<items.length;i++)
		{
			items[i]=c[i].getLeafName();
		}
		ViewUtilites.set_comboBox(view.getCategorycombo(), items);
	}
	public void setSellerNameCombo()
	{
		SellerInfoDao sd=new SellerInfoImp();
		SellerInfo[] s=sd.retrieveAll();
		String[] items=new String[s.length];
		for(int i=0;i<items.length;i++)
		{
			items[i]=s[i].getSellerName();
		}
		ViewUtilites.set_comboBox(view.getSellernamecombo(), items);
	}
	
	public void reactOnFilter()
	{
		Date fromdate=ViewUtilites.convert(view.getLblClick().getText());
		Date todate=ViewUtilites.convert(view.getLblClick_1().getText());
		String catname=view.getCategorycombo().getSelectedItem().toString();
		String sellername=view.getSellernamecombo().getSelectedItem().toString();
		PurchaseDao pd=new PurchaseImp();
		Purchase[] p=pd.retrieveOnMultCond(fromdate, todate, catname, sellername);
		Vector<AccountReportItems> items=new Vector<AccountReportItems>();
		for(int i=0;i<p.length;i++)
		{
			AccountReportItems temp=new AccountReportItems();
			temp.setmCode(p[i].getmCode());
			double amt=p[i].getPurchasePrice()*p[i].getPurchaseQuant();
			temp.setPurchaseAmt(amt);
			temp.setPurchaseDate(p[i].getPurchaseDate());
			temp.setPurchasePrice(p[i].getPurchasePrice());
			temp.setPurchaseQuant(p[i].getPurchaseQuant());
			temp.setSellerId(p[i].getSellerId());
			items.add(temp);
		}
		DefaultTableModel dm=(DefaultTableModel) view.getTable().getModel();
		for(int j=0;j<items.size();j++)
		{
			dm.addRow(new Object[]{items.get(j).getmCode(),items.get(j).getSellerId(),items.get(j).getPurchasePrice(),
			items.get(j).getPurchaseDate(),items.get(j).getPurchaseAmt(),items.get(j).getSellAmt(),items.get(j).getPurchaseQuant(),items.get(j).getRemainingQuant()});
		}
		
		
	}
	

}
